package test1;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.apache.shiro.util.ThreadContext;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

public class testa {

	@Test
	public void testHelloWorld(){
		Factory<SecurityManager> factory= new IniSecurityManagerFactory("classpath:shiro.ini");
		SecurityManager securityManager= factory.getInstance();
		SecurityUtils.setSecurityManager(securityManager);
		Subject subject= SecurityUtils.getSubject();
		UsernamePasswordToken token= new UsernamePasswordToken("zhang","123");
		
		try{
			subject.login(token);
			System.out.println("登陆成功!");
		}catch(Exception e){
			System.out.println("登陆失败!");
			e.printStackTrace();
		}
		Assert.assertEquals(true,subject.isAuthenticated());
		
		System.out.println(subject.isAuthenticated());
		subject.logout();
		
	}
	
	@Test
	public void testCustomRealm(){
		Factory<SecurityManager> factory= new IniSecurityManagerFactory("classpath:shiro-realm.ini");
		SecurityManager securityManager= factory.getInstance();
		SecurityUtils.setSecurityManager(securityManager);
		Subject subject= SecurityUtils.getSubject();
		UsernamePasswordToken token= new UsernamePasswordToken("zhang","123");
		
		try{
			subject.login(token);
			System.out.println("登陆成功!");
		}catch(Exception e){
			System.out.println("登陆失败!");
			e.printStackTrace();
		}
		Assert.assertEquals(true, subject.isAuthenticated());
		subject.logout();
	}
	
	@Test
	public void testMutipleRealm(){
		Factory<SecurityManager> factory= new IniSecurityManagerFactory("classpath:shiro-multiple-realm.ini");
		SecurityManager securityManager= factory.getInstance();
		SecurityUtils.setSecurityManager(securityManager);
		Subject subject= SecurityUtils.getSubject();
		
		UsernamePasswordToken token= new UsernamePasswordToken("wang","123");
		
		try{
			subject.login(token);
			System.out.println("登陆成功!");
		}catch(Exception e){
			System.out.println("登陆失败!");
			e.printStackTrace();
		}
		Assert.assertEquals(true, subject.isAuthenticated());
		subject.logout();
	}
	
	@Test
	public void testJDBCRealm(){
		Factory<SecurityManager> factory= new IniSecurityManagerFactory("classpath:shiro-jdbc-realm.ini");
		SecurityManager securityManager= factory.getInstance();
		SecurityUtils.setSecurityManager(securityManager);
		Subject subject= SecurityUtils.getSubject();
		
		UsernamePasswordToken token= new UsernamePasswordToken("zhang","123");
		
		try{
			subject.login(token);
			System.out.println("登陆成功!");
		}catch(Exception e){
			System.out.println("登陆失败!");
			e.printStackTrace();
		}
		Assert.assertEquals(true, subject.isAuthenticated());
		subject.logout();
	}
	
	@Test
	public void testAllSuccessfulStrategyWithSuccess(){
		//login("classpath:shiro-allautheticator-allsuccess.ini");
		Subject subject= login("classpath:shiro-allautheticator-allsuccess.ini");
		System.out.println(subject.getPrincipal());
		PrincipalCollection principalCollection= subject.getPrincipals();
		for(int i=principalCollection.asList().size()-1;i>=0;i--){
			System.out.println(principalCollection.asList().get(i).toString());	
		}
		Assert.assertEquals(2,principalCollection.asList().size());
	}
	
	@Test
	public void testAllFailStrategyWithSuccess(){
		//login("classpath:shiro-allautheticator-allsuccess.ini");
		Subject subject= login("classpath:shiro-allauthenticator-all-fail.ini");
		System.out.println(subject.getPrincipal());
		PrincipalCollection principalCollection= subject.getPrincipals();
		System.out.println(principalCollection.asList().size());
		for(int i=principalCollection.asList().size()-1;i>=0;i--){
			System.out.println(principalCollection.asList().get(i).toString());	
		}
		Assert.assertEquals(2,principalCollection.asList().size());
	}
	
	@Test
	public void testAtLeastOneStrategyWithSuccess(){
		//login("classpath:shiro-allautheticator-allsuccess.ini");
		Subject subject= login("classpath:shiro-allauthenticator-atleastone-success.ini");
		System.out.println(subject.getPrincipal());
		PrincipalCollection principalCollection= subject.getPrincipals();
		System.out.println(principalCollection.asList().size());
		/*for(int i=principalCollection.asList().size()-1;i>=0;i--){
			System.out.println(principalCollection.asList().get(i).toString());	
		}
		Assert.assertEquals(2,principalCollection.asList().size());*/
		
	}
	
	@Test
	public void testFirstOneStrategyWithSuccess(){
		//login("classpath:shiro-allautheticator-allsuccess.ini");
		Subject subject= login("classpath:shiro-allauthenticator-first-success.ini");
		//System.out.println(subject.getPrincipal());
		PrincipalCollection principalCollection= subject.getPrincipals();
		System.out.println(principalCollection.asList().size());
		/*for(int i=principalCollection.asList().size()-1;i>=0;i--){
			System.out.println(principalCollection.asList().get(i).toString());	
		}*/
		//Assert.assertEquals(2,principalCollection.asList().size());
	}
	
	@Test
	public void testAtLeastTwoStrategyWithSuccess(){
		//login("classpath:shiro-allautheticator-allsuccess.ini");
		Subject subject= login("classpath:shiro-allauthenticator-atleasttwo-success.ini");
		//System.out.println(subject.getPrincipal());
		PrincipalCollection principalCollection= subject.getPrincipals();
		System.out.println(principalCollection.asList().size());
		/*for(int i=principalCollection.asList().size()-1;i>=0;i--){
			System.out.println(principalCollection.asList().get(i).toString());	
		}
		Assert.assertEquals(2,principalCollection.asList().size());*/
	}
	
	@Test
	public void testOnlyOneStrategyWithSuccess(){
		//login("classpath:shiro-allautheticator-allsuccess.ini");
		Subject subject= login("classpath:shiro-allauthenticator-only-success.ini");
		//System.out.println(subject.getPrincipal());
		PrincipalCollection principalCollection= subject.getPrincipals();
		System.out.println(principalCollection.asList().size());
		/*for(int i=principalCollection.asList().size()-1;i>=0;i--){
			System.out.println(principalCollection.asList().get(i).toString());	
		}
		Assert.assertEquals(2,principalCollection.asList().size());*/
	}
	
	private Subject login(String address){
		Factory<SecurityManager> factory= new IniSecurityManagerFactory(address);
		SecurityManager securityManager= factory.getInstance();
		SecurityUtils.setSecurityManager(securityManager);
		Subject subject= SecurityUtils.getSubject();
		
		UsernamePasswordToken token= new UsernamePasswordToken("zhang","123");
		//System.out.println(token.getPrincipal().toString());
		try{
			subject.login(token);
			//System.out.println(token.getPrincipal().toString());
			System.out.println("登陆成功!");
		}catch(Exception e){
			System.out.println("登陆失败!");
			e.printStackTrace();
		}
		//Assert.assertEquals(true, subject.isAuthenticated());
		//subject.logout();
		return subject;
	}
	
	@After
	public void tearDown()throws Exception{
		ThreadContext.unbindSubject();
	}
}
